﻿Ext.define('I2R.controller.ProposalController', {
    extend: 'I2R.controller.BaseController',
    models: 'proposaltreeitem',
    views: [
        'myproposals',
        'proposals',
        'singleproposal',
        'groupproposal',
        'search',
        'memberselect'
    ],
    refs: [
        { ref: 'singleproposal', selector: '#singleproposalform' }
    ],
    stores: ['proposals'],
    init: function () {

        var me = this;

        var home = me.getController('HomeController');

        this.control({

            'button[action=groupproposal]': {
                click: function (btn, e) {
                    home.addActionTab(btn.text, btn.action);
                }
            },

            'button[action=singleproposal]': {
                click: function (btn, e) {
                    home.addActionTab(btn.text, btn.action);
                }
            },

            'button[action=selectmembers]': {
                click: function (btn, e) {

                    var win = Ext.widget('i2rmemberselect', {
                        width: 500,
                        height: 300,
                        title: 'Select the groupmemebers',
                        modal: true,
                        draggable: false
                    });

                    win.show();
                }
            },

            'button[action=upload-files]': {
                click: function (btn, e) {

                    var form = btn.up('form').getForm();

                    if (form.isValid()) {
                        form.submit({
                            url: '../Proposal/SaveFile',
                            waitMsg: 'Uploading file..',
                            success: function (form, action) {

                                // show the files that has been uploaded.
                                var uplPanel = Ext.getCmp('uploadedfile-container');

                                console.log(action.result.data.filename);
                                var fName = action.result.data.filename;

                                console.log(action.result.data.extension);
                                var extension = action.result.data.extension;

                                console.log(action.result.data.temporaryFilename);
                                var tmpName = action.result.data.temporaryFilename;

                                var html = '<div class="' + extension + '">' + fName + '</div>';

                                if (uplPanel.body) {
                                    uplPanel.body.insertHtml("beforeEnd", html);
                                } else {
                                    uplPanel.html += html;
                                }

                                uplPanel.doLayout();

                                // build up the string for all the uploaded files for this deviation.
                                var filecontainer = Ext.getCmp('uploadedfiles');
                                var fcVal = filecontainer.getValue();

                                if (Ext.isEmpty(fcVal)) {
                                    filecontainer.setValue(tmpName + '|' + fName);
                                } else {
                                    filecontainer.setValue(fcVal + ',' + tmpName + '|' + fName);
                                }

                            }
                        });
                    }
                }
            },

            'button[action=save-draft]': {
                click: function (btn, e) {
                    var frm = this.getSingleproposal().getForm();

                    if (frm.isValid()) {
                        frm.submit({
                            method: 'POST',
                            url: '../Proposal/SaveDraft',
                            success: function (form, action) {

                                Ext.Msg.alert('Success', 'Successfully saved!');
                                var idField = Ext.getCmp('deviationId');
                                idField.setValue(action.result.data.Id);

                            },
                            failure: function (form, action) {
                                Ext.Msg.alert('Failed', action.result.data.errorMessage);
                            }
                        });
                    }
                }
            },

            'button[action=save-submit]': {
                click: function (btn, e) {

                    var frm = this.getSingleproposal().getForm();

                    if (frm.isValid()) {
                        frm.submit({
                            method: 'POST',
                            url: '../Proposal/SaveAndSubmit',
                            success: function (form, action) {

                                Ext.Msg.alert('Success', 'Successfully saved!');
                                var idField = Ext.getCmp('deviationId');
                                idField.setValue(action.result.data.Id);

                            },
                            failure: function (form, action) {
                                Ext.Msg.alert('Failed', action.result.data.errorMessage);
                            }
                        });
                    }
                }
            },

            '#proposals-tree': {
                itemclick: function (tree, record, item, index, e, eOpts) {

                    var grid = Ext.getCmp('my-proposals-grid');
                    var id = record.get('id');

                    grid.store.load({
                        params: {
                            proposaltype: record.get('id')
                        }
                    });

                    if (id != 'All') {
                        grid.columns[2].setVisible(false);
                    } else {
                        grid.columns[2].setVisible(true);
                    }

                    grid.setTitle(record.get('text'));
                }
            }

        });

        //                 this.application.on({
        //                 });
    }
});